/**    
 * @{#} ItemView.java Create on 2014-10-9 下午10:04:16    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.evan.common.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.skinapp.R;

import com.evan.common.utils.CommonUtility;

public class ItemView extends LinearLayout {

	private Activity mContext;

	// 选项文本，选项值
	private TextView mTextTip, mTextValue;

	// 右侧箭头
	private ImageView mImageViewForward;

	/**
	 * @param context
	 */
	public ItemView(Context context) {
		super(context);
		this.mContext = (Activity) context;
		init(null);
	}

	public ItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = (Activity) context;
		init(attrs);
	}

	public void addTextChangedListener(TextWatcher watcher) {
		mTextValue.addTextChangedListener(watcher);
	}

	private void init(AttributeSet attrs) {
		RelativeLayout layout = (RelativeLayout) inflate(mContext,
				R.layout.layout_item_view, null);
		TypedArray types = mContext.obtainStyledAttributes(attrs,
				R.styleable.myAttr, 0, 0);

		int marginLeft = (int) types.getDimension(
				R.styleable.myAttr_marginLeft, 0);
		int paddingLeft = types.getDimensionPixelOffset(
				R.styleable.myAttr_paddingLeft, 0);
		boolean needArrow = types.getBoolean(R.styleable.myAttr_need_arrow,
				true);
		String text = types.getString(R.styleable.myAttr_text);
		
		//-13390944
//		int textColor = types.getColor(R.styleable.myAttr_textColor, -1);
		String textColor = types.getString(R.styleable.myAttr_textColor);
		String textValutColor = types.getString(R.styleable.myAttr_text_value_color);
		
//		Color.parseColor(colorString)
		
		
		int textSize = (int) types
				.getDimension(R.styleable.myAttr_textSize, 17);
		String textValueHint = types
				.getString(R.styleable.myAttr_text_value_hint);

		mTextTip = (TextView) layout.findViewById(R.id.text_tip);
		mTextValue = (TextView) layout.findViewById(R.id.text_value);
		mImageViewForward = (ImageView) layout
				.findViewById(R.id.img_list_forward);
		android.widget.RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) mTextTip
				.getLayoutParams();

		if (marginLeft > 0) {
			params.leftMargin = marginLeft;
		}
		if (paddingLeft > 0) {
			mTextTip.setPadding(paddingLeft, 0, 0, 0);
		}
		if (!needArrow) {
			mImageViewForward.setVisibility(View.GONE);
		}
		mTextTip.setText(text);
		if (textColor != null) {
			mTextTip.setTextColor(Color.parseColor(textColor));
		}
		
		mTextTip.setTextSize(textSize);
		mTextTip.setLayoutParams(params);

		if (CommonUtility.isNull(textValueHint)) {
			textValueHint = "请选择";
		}
		
		if(textValutColor != null){
			mTextValue.setTextColor(Color.parseColor(textValutColor));
			mTextValue.setHintTextColor(Color.parseColor(textValutColor));
		}
		
		mTextValue.setHint(textValueHint);
		addView(layout);

		types.recycle();
	}

	public TextView getTextTip() {
		return mTextTip;
	}

	public TextView getTextValue() {
		return mTextValue;
	}

	public String getTextTipValue() {
		return CommonUtility.getText(mTextTip);
	}

	public String getTextValueValue() {
		return CommonUtility.getText(mTextValue);
	}

	public void setTextTipValue(String str) {
		mTextTip.setText(str);
	}

	public void setTextTipValue(int resId) {
		mTextTip.setText(mContext.getResources().getString(resId));
	}

	public void setCompoundDrawables(Drawable left, Drawable top,
			Drawable right, Drawable bottom) {
		mTextTip.setCompoundDrawables(left, top, right, bottom);
	}

	public void setTextValueValue(String str) {
		mTextValue.setText(str);
	}

	public void setTextValueValue(int resId) {
		mTextValue.setText(mContext.getResources().getString(resId));
	}

}
